package zzs.generalframework.coder.core.encoder.temp.console.dao;

import java.util.List;

import zzs.generalframework.coder.common.model.ClazzBO;
import zzs.generalframework.common.core.authorization.authfunction.AuthFunctionPO;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-coder
 * @package zzs.generalframework.coder.core.encoder.temp.console.dao
 * @author zzs
 * @date 2018年1月31日
 * @version 1.0
 */
public class ImplementCoder
{

	public String createFile(ClazzBO clazzBO)
	{
		StringBuffer returnBuffer = new StringBuffer();

		returnBuffer.append("private Logger logger = LoggerFactory.getLogger(").append(clazzBO.getClassName()).append(".class);").append("\n");

		// @PersistenceContext(unitName = "zzs-entity")
		// EntityManager entityManager;
		returnBuffer.append("@PersistenceContext(unitName = \"zzs-entity\")").append("\n");
		returnBuffer.append("EntityManager entityManager;").append("\n");

		// @Autowired
		// JdbcTemplate jdbcTemplate;
		returnBuffer.append("@Autowired").append("\n");
		returnBuffer.append("JdbcTemplate jdbcTemplate;").append("\n");

		// @Override
		// public boolean create(DimissionTypePO dimissionTypePO)
		// {
		// entityManager.persist(dimissionTypePO);
		// return true;
		// }
		returnBuffer.append("@Override").append("\n");
		returnBuffer.append("public boolean insert(").append(clazzBO.getEntityCode()).append("PO ").append(clazzBO.getEntityCodeCamal()).append("PO)").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("logger.info(\"====插入").append(clazzBO.getEntityName()).append("数据开始====\");").append("\n");
		returnBuffer.append("logger.debug(\"====插入").append(clazzBO.getEntityName()).append("数据,入参:").append(clazzBO.getEntityCode()).append("PO=[{}]====\", ").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");
		returnBuffer.append("entityManager.persist(").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");
		returnBuffer.append("logger.debug(\"====插入").append(clazzBO.getEntityName()).append("数据,出参:boolean=[{}]====\", true);").append("\n");
		returnBuffer.append("logger.info(\"====插入").append(clazzBO.getEntityName()).append("数据结束====\");").append("\n");
		returnBuffer.append("return true;").append("\n");
		returnBuffer.append("}").append("\n");

		// @Override
		// public boolean update(DimissionTypePO dimissionTypePO)
		// {
		// entityManager.merge(dimissionTypePO);
		// return true;
		// }
		returnBuffer.append("@Override").append("\n");
		returnBuffer.append("public boolean update(").append(clazzBO.getEntityCode()).append("PO ").append(clazzBO.getEntityCodeCamal()).append("PO)").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("logger.info(\"====更新").append(clazzBO.getEntityName()).append("数据开始====\");").append("\n");
		returnBuffer.append("logger.debug(\"====更新").append(clazzBO.getEntityName()).append("数据,入参:").append(clazzBO.getEntityCode()).append("PO=[{}]====\", ").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");
		returnBuffer.append("entityManager.merge(").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");
		returnBuffer.append("logger.debug(\"====更新数据,出参:boolean=[{}]====\", true);").append("\n");
		returnBuffer.append("logger.info(\"====更新").append(clazzBO.getEntityName()).append("数据结束====\");").append("\n");
		returnBuffer.append("return true;").append("\n");
		returnBuffer.append("}").append("\n");

		// @Override
		// public List<DimissionTypePO> findByConditions(DimissionTypeInfo
		// dimissionTypeInfo)
		// {
		// StringBuffer sqlBuffer = new StringBuffer();
		// QueryParameter parameter = new QueryParameter();
		// sqlBuffer.append("select a from DimissionTypePO a where 1=1 ");
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypeInfo.getDimissionTypeID()))
		// {
		// sqlBuffer.append(" and a.dimissionTypeID = ?");
		// parameter.putParameterObject(dimissionTypeInfo.getDimissionTypeID());
		// }
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypeInfo.getDimissionTypeCode()))
		// {
		// sqlBuffer.append(" and a.dimissionTypeCode = ?");
		// parameter.putParameterObject(dimissionTypeInfo.getDimissionTypeCode());
		// }
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypeInfo.getDimissionTypeName()))
		// {
		// sqlBuffer.append(" and a.dimissionTypeName = ?");
		// parameter.putParameterObject(dimissionTypeInfo.getDimissionTypeName());
		// }
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypeInfo.getRecordStatus()))
		// {
		// sqlBuffer.append(" and a.recordStatus = ?");
		// parameter.putParameterObject(dimissionTypeInfo.getRecordStatus());
		// }
		// Query query = entityManager.createQuery(sqlBuffer.toString());
		// parameter.setQueryParameter(query);
		// List<DimissionTypePO> resultList = query.getResultList();
		// return resultList;
		// }

		returnBuffer.append("@Override").append("\n");
		returnBuffer.append("public List<").append(clazzBO.getEntityCode()).append("PO> select(").append(clazzBO.getEntityCode()).append("Info ").append(clazzBO.getEntityCodeCamal()).append("Info)").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("logger.info(\"====查询").append(clazzBO.getEntityName()).append("数据开始====\");").append("\n");
		returnBuffer.append("logger.debug(\"====查询").append(clazzBO.getEntityName()).append("数据,入参:").append(clazzBO.getEntityCode()).append("Info=[{}]====\", ").append(clazzBO.getEntityCodeCamal()).append("Info);").append("\n");
		returnBuffer.append("List<").append(clazzBO.getEntityCode()).append("PO> returnList = null;").append("\n");
		returnBuffer.append("StringBuffer sqlBuffer = new StringBuffer();").append("\n");
		returnBuffer.append("QueryParameter parameter = new QueryParameter();").append("\n");
		returnBuffer.append("sqlBuffer.append(\"select a from ").append(clazzBO.getEntityCode()).append("PO a where 1=1 \");").append("\n");

		returnBuffer.append("if (StringUtils.isNotNullAndBlank(").append(clazzBO.getEntityCodeCamal()).append("Info.get").append(clazzBO.getEntityCode()).append("ID()))").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("sqlBuffer.append(\" and a.").append(clazzBO.getEntityCodeCamal()).append("ID=?\");").append("\n");
		returnBuffer.append("parameter.putParameterObject(").append(clazzBO.getEntityCodeCamal()).append("Info.get").append(clazzBO.getEntityCode()).append("ID());").append("\n");
		returnBuffer.append("}").append("\n");

		returnBuffer.append("logger.info(\"查询数据SQL:[{}]\", sqlBuffer.toString());").append("\n");
		returnBuffer.append("Query query = entityManager.createQuery(sqlBuffer.toString());").append("\n");
		returnBuffer.append("parameter.setQueryParameter(query);").append("\n");
		returnBuffer.append("returnList = query.getResultList();").append("\n");
		returnBuffer.append("logger.debug(\"====查询").append(clazzBO.getEntityName()).append("数据,出参:List<").append(clazzBO.getEntityCode()).append("PO>=[{}]====\", ").append("returnList);").append("\n");
		returnBuffer.append("logger.info(\"====查询").append(clazzBO.getEntityName()).append("数据结束====\");").append("\n");
		returnBuffer.append("return returnList;").append("\n");
		returnBuffer.append("}").append("\n");

		// @Override
		// public boolean remove(DimissionTypePO dimissionTypePO)
		// {
		// entityManager.remove(dimissionTypePO);
		// return true;
		// }
		returnBuffer.append("@Override").append("\n");
		returnBuffer.append("public boolean delete(").append(clazzBO.getEntityCode()).append("PO ").append(clazzBO.getEntityCodeCamal()).append("PO)").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("logger.info(\"====删除").append(clazzBO.getEntityName()).append("数据开始====\");").append("\n");
		returnBuffer.append("logger.debug(\"====删除").append(clazzBO.getEntityName()).append("数据,入参:").append(clazzBO.getEntityCode()).append("PO=[{}]====\", ").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");

		returnBuffer.append("entityManager.remove(").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");
		returnBuffer.append("logger.debug(\"====删除").append(clazzBO.getEntityName()).append("数据,出参:boolean=[{}]====\", true);").append("\n");
		returnBuffer.append("logger.info(\"====删除").append(clazzBO.getEntityName()).append("数据结束====\");").append("\n");

		returnBuffer.append("return true;").append("\n");
		returnBuffer.append("}").append("\n");

		// @Override
		// public Long count(DimissionTypePO dimissionTypePO)
		// {
		// StringBuffer sqlBuffer = new StringBuffer();
		// QueryParameter parameter = new QueryParameter();
		// sqlBuffer.append("select count(*) from DimissionTypePO a where 1=1
		// ");
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypePO.getDimissionTypeID()))
		// {
		// sqlBuffer.append(" and a.dimissionTypeID = ?");
		// parameter.putParameterObject(dimissionTypePO.getDimissionTypeID());
		// }
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypePO.getDimissionTypeCode()))
		// {
		// sqlBuffer.append(" and a.dimissionTypeCode = ?");
		// parameter.putParameterObject(dimissionTypePO.getDimissionTypeCode());
		// }
		// if
		// (StringUtils.isNotNullAndBlank(dimissionTypePO.getDimissionTypeName()))
		// {
		// sqlBuffer.append(" and a.dimissionTypeName = ?");
		// parameter.putParameterObject(dimissionTypePO.getDimissionTypeName());
		// }
		// if (StringUtils.isNotNullAndBlank(dimissionTypePO.getRecordStatus()))
		// {
		// sqlBuffer.append(" and a.recordStatus = ?");
		// parameter.putParameterObject(dimissionTypePO.getRecordStatus());
		// }
		// Query query = entityManager.createQuery(sqlBuffer.toString());
		// parameter.setQueryParameter(query);
		// Long resultCount = (Long) query.getSingleResult();
		// return resultCount;
		// }
		returnBuffer.append("@Override").append("\n");
		returnBuffer.append("public Long count(").append(clazzBO.getEntityCode()).append("PO ").append(clazzBO.getEntityCodeCamal()).append("PO)").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("logger.info(\"====统计").append(clazzBO.getEntityName()).append("数据开始====\");").append("\n");
		returnBuffer.append("logger.debug(\"====统计").append(clazzBO.getEntityName()).append("数据,入参:").append(clazzBO.getEntityCode()).append("PO=[{}]====\", ").append(clazzBO.getEntityCodeCamal()).append("PO);").append("\n");
		returnBuffer.append("Long returnLong = null;").append("\n");
		returnBuffer.append("StringBuffer sqlBuffer = new StringBuffer();").append("\n");
		returnBuffer.append("QueryParameter parameter = new QueryParameter();").append("\n");
		returnBuffer.append("sqlBuffer.append(\"select count(*) from ").append(clazzBO.getEntityCode()).append("PO a where 1=1 \");").append("\n");

		returnBuffer.append("if (StringUtils.isNotNullAndBlank(").append(clazzBO.getEntityCodeCamal()).append("PO.get").append(clazzBO.getEntityCode()).append("ID()))").append("\n");
		returnBuffer.append("{").append("\n");
		returnBuffer.append("sqlBuffer.append(\" and a.").append(clazzBO.getEntityCodeCamal()).append("ID=?\");").append("\n");
		returnBuffer.append("parameter.putParameterObject(").append(clazzBO.getEntityCodeCamal()).append("PO.get").append(clazzBO.getEntityCode()).append("ID());").append("\n");
		returnBuffer.append("}").append("\n");

		returnBuffer.append("logger.info(\"统计数据SQL:[{}]\", sqlBuffer.toString());").append("\n");
		returnBuffer.append("Query query = entityManager.createQuery(sqlBuffer.toString());").append("\n");
		returnBuffer.append("parameter.setQueryParameter(query);").append("\n");
		returnBuffer.append("returnLong = (Long) query.getSingleResult();").append("\n");
		returnBuffer.append("logger.debug(\"====统计").append(clazzBO.getEntityName()).append("数据,出参:Long=[{}]====\", returnLong);").append("\n");
		returnBuffer.append("logger.info(\"====统计").append(clazzBO.getEntityName()).append("数据结束====\");").append("\n");

		returnBuffer.append("return returnLong;").append("\n");
		returnBuffer.append("}").append("\n");

		return returnBuffer.toString();

	}

}
